package borge.web.teamspirit.client;

import borge.web.teamspirit.client.page.PagePanel;
import borge.web.teamspirit.client.page.main.panels.MainPagePanel;
import borge.web.teamspirit.client.page.team.TokenData;
import borge.web.teamspirit.client.page.team.panels.TeamPagePanel;
import borge.web.teamspirit.client.servercomm.ServerComm;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * Implements PanelHandler to control the different panels
 * for the different purposes
 * @author Andreas Borglin
 */
public class TeamSpirit implements EntryPoint, ValueChangeHandler<String>{

	private MainPagePanel iMainPage;
	private TeamPagePanel iTeamPage;
	
	private PagePanel iCurPagePanel;
	
	private RootPanel iRootPanel;
	
	private static TeamSpiritConstants iConstants;
	
	static{
		iConstants = GWT.create(TeamSpiritConstants.class);
	}
	
	public static TeamSpiritConstants getConstants(){
		return iConstants;
	}
	
	public TeamSpirit(){
	
		
	}

	public void onModuleLoad() {

		iRootPanel = RootPanel.get();
		iRootPanel.setStyleName("root");
		//root.setStyleName("MainPanel");
		//root.add(iSuperPanel);
		//root.add(iTeamPage);
		
		if(History.getToken().length() == 0){
			History.newItem(TokenData.MAIN_MODE);
		}
		
		History.addValueChangeHandler(this);	
		History.fireCurrentHistoryState();
		//iMainPage = new MainPagePanel();
		//iTeamPage = new TeamPagePanel();
	}

	private void switchPage(PagePanel aPanel){
		
		if(iCurPagePanel != null)
			iRootPanel.remove(iCurPagePanel);
		
		iCurPagePanel = aPanel;
		iCurPagePanel.setStyleName("body");
		iRootPanel.add(iCurPagePanel);
	}
	
	private PagePanel createPageIfNull(String aToken){
		
		PagePanel panel = null;
		
		if(aToken.equals(TokenData.MAIN_MODE)){
			
			if(iMainPage == null)
				iMainPage = new MainPagePanel();
			panel = iMainPage;
		}
		else if(aToken.equals(TokenData.TEAM_MODE)){
			
			if(iTeamPage == null)
				iTeamPage = new TeamPagePanel();
			panel = iTeamPage;
		}
		else if(aToken.equals(TokenData.ASSOCIATION_MODE)){
			
		}
		else{
			// error!
			Window.alert("Unknown token: " + aToken);
		}
		
		return panel;
	}

	private void parseToken(String aToken){
		
		//Window.alert("token =)
		int pageEnd = aToken.indexOf(TokenData.TOKENIZER);
		if(pageEnd == -1){
			pageEnd = aToken.length();
		}
		
		String pageStr = aToken.substring(0, pageEnd);
		// TODO: remove later..for testing only
		if(pageStr.equals(TokenData.TEST_CREATE)){
			ServerComm.testCreate();
			return;
		}
		else if(pageStr.equals(TokenData.TEST_PRINT)){
			ServerComm.testPrint();
			return;
		}
		
		SessionData.sBaseToken = aToken;
		PagePanel page = createPageIfNull(pageStr);
		
		if(page != null){
			switchPage(page);
			if(pageEnd != aToken.length())
				page.handleToken(aToken.substring(pageEnd + 1, aToken.length()));
		}
		

	}
	
	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		// TODO Auto-generated method stub

		//Window.alert("history in teamspirit = " + event.getValue());
		parseToken(event.getValue());
		
	}

}
